from typing import Any, Dict

from bcat.rpc.server.server import Server
from jsonrpcserver import dispatch
from flask.views import MethodView
from flask import request, Response
class FlaskServerWrapper(Server):
    services = {}
    class Api(MethodView):
        def post(self):
            req = request.get_data().decode()
            print(req)
            return Response(dispatch(req, FlaskServerWrapper.services), \
                content_type="application/json" \
            ) 
        #end def
    #end Api

    def __init__(self, server: Server):
        super().__init__(server.url() + '/api')
        self.__server = server
        print(self.url())


    def register(self, method: str, f: Any) -> bool:
        FlaskServerWrapper.services[method] = f 
        return True

    def unregister(self, method: str) -> bool:
        del FlaskServerWrapper.services[method]
        return True
    
    def run(self) -> bool:
        self.__server.register('/api', FlaskServerWrapper.Api.as_view('api'))
        self.__server.run()
        return True
